<?php
/**
 * Template.php
 * -------------------------
 * Implements a pull-based templating similar to OpenACS
 * author: Carmelo Capinpin (mello[dot]capinpin[at]gmail[dot]com)
 * date-created: 2008-10-16
 * date-updated: 2008-12-08 mello "add constructor"
 *
**/
class Template
{
	var $CI;

	function Template()
	{
		$this->CI =& get_instance();
	}

    function render($view, $data = array())
	{
        $data['content'] = $this->CI->load->view($view, $data, true);
        $ancestors = explode('/', $view);

        while ($ancestors)
		{
			/* move to upper level */
            unset($ancestors[count($ancestors)-1]);
			/* ancestor template location */
            $ancestor = implode('/', $ancestors);
			/* ancestor template path */
            $template = $ancestor . '/master.php';
			/* check if ancestor template exist */
            if (file_exists(APPPATH . '/views/' . $template))
			{
				/* reder content to ancestor template */
                $data['content'] = $this->CI->load->view($template, $data, true);
            }
        }
        echo $data['content'];
    }

	function block($content)
	{
		if (file_exists(APPPATH . 'views' . THEME . '/block.php'))
		{
			$content = $this->CI->load->view(THEME.'/block', array('content'=>$content), true);
		}
		return $content;
	}

	function widget($view, $data = array())
	{
		$content = $this->CI->load->view($view, $data, true);
		return $this->block($content);
	}
}

/* END:Template.php */